package com.example.demo;

import com.alibaba.excel.util.StringUtils;
import com.example.demo.model.ProductAttributeNameDTO;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author FENG
 * @description TODO
 * @date 2023/8/11 10:03
 */
public class Test {

    public static String emptyHori(Long source) {

        return source == null ? "--" : String.valueOf(source);
    }

    public static List<String> stringArrayList = new ArrayList<>();

    static {
        stringArrayList.add("SHC202311904700-04");
    }


    public static final String SHACTS = "SHACTS";

    public static void main(String[] args) throws ParseException {
        StringBuffer sb = new StringBuffer();
        sb.append(" select ua.user_achievement_id, ua.contact_id, ua.user_id, ua.product_id, ua.xproject_contact_id,ua.assign_Position, ");
        sb.append(" ua.payment_basic, ua.plus_percent, ua.plus_value, soi.sure_release_status, ");
        sb.append(" soi.sub_order_payment_money, soi.sub_order_sale_money, soi.sub_order_status, soi.project_radix_id AS projectradixid ");
        sb.append(" FROM user_achievement ua ");
        sb.append(" LEFT JOIN user_release ur ON ua.USER_ACHIEVEMENT_ID = ur.USER_ACHIEVEMENT_ID");
        sb.append(" LEFT JOIN sub_order_info soi ON ua.xproject_contact_id = soi.id");
        sb.append(" where 1=1 ");
        sb.append(" and (ur.STATUS = '锁定' OR ur.STATUS = '已发放')");
        sb.append(" and ur.plus_value > 0 and ua.rec_status = 'A' ");   // 过滤逻辑删除
        sb.append(" and ur.RELEASE_AMOUNT > 0  ");
        //sb.append(" AND ur.trigger_date >='2023-09-01' ");  //已发放明细去掉可锁定日期
        sb.append(" AND soi.sub_order_sale_money != ur.plus_value ");  //    子订单销售额 不等于 个人提成基数 规则引擎
        sb.append(" AND soi.refund_money > 0 AND soi.rec_status = 'A' ");  //    子订单退费金额>0 规则引擎

        sb.append(" GROUP BY ur.USER_ACHIEVEMENT_ID  ");
        sb.append(" ORDER BY ua.create_date DESC");
        System.out.println(sb);

    }

    public static String getMaxAfterSaleNo(String orderNo) {

        List<String> result = stringArrayList;
        if (result == null) {
            result = new ArrayList<>();
        }
        System.out.println("==========getMaxAfterSaleNo," + result);
        orderNo = orderNo.replace("BS", "").replace("P", "").replace("ACTS", ""); // 去掉 原订单字母
        int i = result.size() + 1;
        String binary = result.size() < 10 ? "0" : "";
        return "SH" + orderNo + "-" + binary + i;

    }


    private static String getOrderNoByAfterSaleNo(String afterSaleNo) {
        //SHACTSBS-202312061814524881-01  BS-202312021751404881
        if (!StringUtils.isEmpty(afterSaleNo)) {
            String[] dd = afterSaleNo.replace(SHACTS, "").split("-");
            return dd[0] + "-" + dd[1];
        }
        return afterSaleNo;
    }

    public static String strinstanceof(Object data) {
        String c = "";
        if (data instanceof String) {
            c = (String) data;
        } else if (data instanceof Integer) {
            c = data + "";
        } else if (data instanceof Double) {
            c = data + "";
        } else if (data instanceof Object) {
            c = (String) data;
        }
        return c;
    }


    public static void getInvoiceInfoByOrderCode(List<String> orderIds, List<String> orderCodes) {

        StringBuffer sql = new StringBuffer();
        sql.append("SELECT ifnull(invo.XPROJECT_CONTACT_ID, invofee.order_id)  orderId, invo.invoice_xpc_id  invoiceId, " +
                "          invo.INVOICE_AMOUNT invoiceAmount,invo.CONTACT_PHONE contactPhone,invofee.unit_price unitPrice, " +
                "          invofee.fee_id feeId, invofee.order_code orderCode " +
                " FROM binvoice_xpc invo LEFT JOIN binvoice_order_fee invofee ON invo.INVOICE_XPC_ID = invofee.invoice_id " +
                " WHERE" +
                "   (" +
                "       invo.XPROJECT_CONTACT_ID IN ( '" + orderIds.stream().collect(Collectors.joining("','")) + "' )" +
                "       or" +
                "       invofee.order_code IN ( '" + orderCodes.stream().collect(Collectors.joining("','")) + "' )" +
                "   )" +
                "   and invo.invoice_status in ('已开发票','审核中','待开发票') order by invo.create_date");
        System.out.println("InvoiceXpcDaoImpl.getInvoiceInfoByOrderCode sql = {}" + sql);

    }

    public static void getOrderInfoByMobile(List mobileList) {

        StringBuffer sql = new StringBuffer();
        //online_order
        sql.append(" SELECT ORDER_CODE orderCode,ORDER_NAME goodsName,CREATE_DATE createDate," +
                "           ONLINE_ORDER_ID orderId, PAY_AMOUNT payAmount, MOBILE contactPhone " +
                "   FROM online_order " +
                "   WHERE REC_STATUS = 'A' and PAY_AMOUNT > 0 " +
                "         AND MOBILE in ('" + String.join("','", mobileList) + "') ");
        //xproject_contact
        sql.append(" union all " +
                " SELECT a.XPROJECT_CODE orderCode, b.`name` goodsName,a.CREATE_DATE createDate, " +
                "        a.XPROJECT_CONTACT_ID orderId, a.PAID_AMOUNT payAmount, c.MOBILE contactPhone  " +
                " FROM xproject_contact a " +
                "      LEFT JOIN product b ON a.PRODUCT_ID = b.PRODUCT_ID " +
                "      LEFT JOIN bcontact c on a.CONTACT_ID = c.CONTACT_ID  " +
                " WHERE a.REC_STATUS = 'A' and  a.PAID_AMOUNT > 0 " +
                "       and c.mobile in ('" + String.join("','", mobileList) + "') ");
        //bcontract
        sql.append(" union all " +
                " SELECT a.contract_code orderCode, a.contract_name goodsName, a.create_date createDate, " +
                "          a.contract_id orderId, a.contract_amount payAmount, b.MOBILE contactPhone " +
                " FROM bcontract a left join bcontact b on a.contact_id = b.CONTACT_ID" +
                " where a.REC_STATUS = 'A' and  a.contract_amount > 0 " +
                "       AND b.MOBILE in ('" + String.join("','", mobileList) + "') ");
        //activity_sign_up
        sql.append(" union all " +
                " SELECT a.ACTIVITY_SIGN_CODE orderCode, b.`name` goodsName, a.SIGN_DATE createDate, " +
                "        a.ACTIVITY_SIGN_ID orderId, a.PAY_AMOUNT payAmount, a.CONTACT_MOBILE contactPhone " +
                " FROM activity_sign_up a LEFT JOIN bactivitys b ON a.ACTIVITY_ID = b.ACTIVITY_ID " +
                " WHERE a.REC_STATUS = 'A' and a.valid_status = 'A' and a.PAY_AMOUNT > 0 and PAY_STATUS = '已付款' " +
                "       and a.CONTACT_MOBILE in ('" + String.join("','", mobileList) + "') ");
        //join_member_order
        sql.append(" union all " +
                " SELECT a.ORDER_CODE orderCode, '会费' goodsName, a.CREATE_DATE createDate, " +
                "        a.order_id orderId, PAYMENT_AMOUNT payAmount, b.mobile contactPhone " +
                " FROM join_member_order a LEFT JOIN bcontact b ON a.CONTACT_ID = b.CONTACT_ID " +
                " WHERE a.REC_STATUS = 'A' and a.PAYMENT_AMOUNT > 0 " +
                "       and b.mobile in ('" + String.join("','", mobileList) + "') " +
                " order by createDate desc");
        System.out.println("getOrderInfoByMobile sql = " + sql);


    }


    public static void diff0() {
        List<String> list1 = Arrays.asList("A", "B", "C", "D1");
        List<String> list2 = Arrays.asList("B", "C", "E", "F", "A", "D");
        List<String> diff = list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList());
        List<String> diff1 = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList());
        //diff.addAll(diff1);
        System.out.println(diff);
        System.out.println(diff1);

    }

    public static void diff1() {
        List<String> list1 = Arrays.asList("apple", "banana", "orange", "pear");
        List<String> list2 = Arrays.asList("apple", "banana", "grape");

        List<String> list3 = Stream.concat(
                        list1.stream().filter(str -> !list2.contains(str)),
                        list2.stream().filter(str -> !list1.contains(str)))
                .collect(Collectors.toList());

        System.out.println("Different strings: " + list3);
    }


    public static void diff2() {
        List<ProductAttributeNameDTO> l1 = Arrays.asList(
                new ProductAttributeNameDTO(1L, "颜色"),
                new ProductAttributeNameDTO(2L, "尺码"),
                new ProductAttributeNameDTO(3L, "重量")
        );
        List<ProductAttributeNameDTO> l2 = Arrays.asList(
                new ProductAttributeNameDTO(1L, "颜色"),
                new ProductAttributeNameDTO(3L, "重量"),
                new ProductAttributeNameDTO(4L, "材质")
        );

        List<ProductAttributeNameDTO> resultList = Stream.concat(l1.stream(), l2.stream())
                .collect(Collectors.groupingBy(ProductAttributeNameDTO::getName, Collectors.counting()))
                .entrySet().stream()
                .filter(e -> e.getValue() == 1)
                .map(Map.Entry::getKey)
                .map(name -> Stream.concat(l1.stream(), l2.stream()).filter(p -> p.getName().equals(name)).findFirst().get())
                .collect(Collectors.toList());
        System.out.println(resultList);
    }

}
